package com.cdzs.twocamera.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;

import java.io.ByteArrayOutputStream;

/**
 * Created by M_genius on 2018/7/24.\
 * 描述:
 */

public class SimpleUVCCameraSurfaceView extends SurfaceView implements Camera.PreviewCallback {

    private final static String TAG = "SurfaceView";

    private static final int FACE_NUM = 1;

    Context mContext;


    //传输五个bitmap数组
    Bitmap[] bitmaps = new Bitmap[5];

    int number = 0;//作为计数器用

    OnFaceCollectListener onFaceCollectListener = null;

    public SimpleUVCCameraSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public SimpleUVCCameraSurfaceView(Context context) {
        this(context, null);
    }

    /**
     * 收集五张图片 监听器
     */
    public interface OnFaceCollectListener {

        /**
         * 收集五张face
         *
         * @param bitmaps 返回的五张脸的数组
         */
        void OnFaceCollected(Bitmap[] bitmaps);

        /**
         * face重新绘制
         *
         * @param isStart 是否重新收集
         */
        void OnFaceCollectStart(boolean isStart);
    }


    //============================== Camera.PreviewCallback ====================
    /***
     * 想在这里做一个监听处理 收五侦 传输出去
     * @param bytes
     * @param camera
     */
    @Override
    public void onPreviewFrame(byte[] bytes, Camera camera) {
        if (number < FACE_NUM) {
            //判断监听器 开始收集
            if (onFaceCollectListener != null) {
                onFaceCollectListener.OnFaceCollectStart(true);
                //有byte数组转为bitmap
                bitmaps[number] = byte2bitmap(bytes, camera);
                Log.d(TAG, "********收集了" + number + "个************");
                number = number + 1;
                if (number == 1) {
                    onFaceCollectListener.OnFaceCollected(bitmaps);
                    onFaceCollectListener.OnFaceCollectStart(true);
                    number = 0;
                }
            }
        } else {
            //不做操作,false 表示，收集结束
            onFaceCollectListener.OnFaceCollectStart(false);
        }
    }
    //============================== Camera.PreviewCallback ====================


    /**
     * 二进制数组转bitmap
     * @param bytes
     * @param camera
     * @return
     */
    private Bitmap byte2bitmap(byte[] bytes, Camera camera) {
        Bitmap bitmap = null;

        Camera.Size size = camera.getParameters().getPreviewSize(); // 获取预览大小
        final int w = size.width; // 宽度
        final int h = size.height;
        final YuvImage image = new YuvImage(bytes, ImageFormat.NV21, w, h, null);
        ByteArrayOutputStream os = new ByteArrayOutputStream(bytes.length);
        if (!image.compressToJpeg(new Rect(0, 0, w, h), 100, os)) {
            return null;
        }
        byte[] tmp = os.toByteArray();

        bitmap = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);

        Matrix matrix = new Matrix();
        //matrix.setRotate(180);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

        return bitmap;
    }
}